<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="searchValue">
        <el-input v-model="queryParams.searchValue" placeholder="请输入字段名称/字段值" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="" prop="fieldType">
        <el-select v-model="queryParams.fieldType" placeholder="请选择类型" clearable style="width: 240px">
          <el-option v-for="dict in dict.type.field_class" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button icon="el-icon-upload2" size="mini" @click="handleImport"
          v-hasPermi="['system:field:import']">导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:field:export']">导出</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="fieldList" @selection-change="handleSelectionChange">
      <el-table-column label="字段类型" align="left" width="200" prop="fieldType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.field_class" :value="scope.row.fieldType" />
        </template>
      </el-table-column>
      <el-table-column label="字段名称" align="left" width="440" prop="fieldName" />
      <el-table-column label="字段值" align="left" prop="fieldVal" />
      <el-table-column label="状态" width="80" align="center" prop="fieldStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_enabled" :value="scope.row.fieldStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.fieldStatus == 0" size="small" @click="handleEnable(scope.row)" icon="el-icon-open"
            v-hasPermi="['system:field:enable']">启用</el-button>
          <el-button v-else size="small" @click="handleUnEnable(scope.row)" icon="el-icon-close"
            v-hasPermi="['system:field:unenable']">停用</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 异动字段导入对话框 -->
    <el-dialog :close-on-click-modal="false" :title="upload.title" :visible.sync="upload.open" width="50%" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false">
        <el-button size="mini" type="primary">点击上传</el-button>
        <el-button size="mini" type="primary" @click.stop="importTemplate">模板下载</el-button>
        <div class="el-upload__tip" slot="tip">
          <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
        </div>
        <div slot="tip" class="el-upload__tip">温馨提示:</div>
        <div slot="tip" class="el-upload__tip">
          1、导入文件格式仅支持xlsx文件类型的Excel文件，单元格格式须设置为"文本"。
        </div>
        <div slot="tip" class="el-upload__tip">
          2、列标题为：规则编码、规则名称、稽核对象、规则类型、规则说明、定时任务执周期
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="upload.open = false">取消</el-button>
        <el-button type="primary" size="mini" @click="submitFileForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listField, getField, delField, updateField } from "@/api/system/field";
import { getToken } from "@/utils/auth";
export default {
  name: "Field",
  dicts: ['field_class', 'sys_enabled'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示查询条件
      showSearch: true,
      // 总条数
      total: 0,
      // 系统异动字段表格数据
      fieldList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fieldType: null,
        searchValue: null,
        rangeStatus: null,
      },
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/auditRule/field/importTemplate",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询系统异动字段列表 */
    getList() {
      this.loading = true;
      listField(this.queryParams).then(response => {
        this.fieldList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fieldType: null,
        fieldName: null,
        fieldValue: null,
        rangeStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 查询按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增";
    },
    /** 编辑按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getField(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "编辑";
      });
    },
    /** 启用操作 */
    handleEnable(row) {
      this.$modal.confirm('是否启用字段【' + row.fieldName + '】').then(function () {
        updateField({ fieldId: row.fieldId, fieldStatus: 1 });
      }).then(() => {
        this.getList();
      }).catch(() => { });
    },
    /** 停用操作 */
    handleUnEnable(row) {
      this.$modal.confirm('是否停用字段【' + row.fieldName + '】').then(function () {
        updateField({ fieldId: row.fieldId, fieldStatus: 0 });
      }).then(() => {
        this.getList();
      }).catch(() => { });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('确定进行删除操作？').then(function () {
        return delField(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/auditRule/field/list/export', {
        ...this.queryParams
      }, `异动字段_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "异动字段导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('/auditRule/field/exportTemplate', {
      }, `异动字段_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
